package com.gkj.gulimall.product.service.impl;

import com.gkj.gulimall.common2.utils.PageUtils;
import com.gkj.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.gkj.gulimall.product.dao.AttrGroupDao;
import com.gkj.gulimall.product.dao.CategoryDao;
import com.gkj.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gkj.gulimall.product.entity.AttrEntity;
import com.gkj.gulimall.product.entity.CategoryEntity;
import com.gkj.gulimall.product.vo.AttrRespVo;
import com.gkj.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkj.common.utils.Query;

import com.gkj.gulimall.product.dao.AttrDao;
import com.gkj.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }


    @Autowired
    AttrAttrgroupRelationDao relationDao;
    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    CategoryDao categoryDao;

    @Transactional
    @Override
    public void saveAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);//属性对拷
        //保存基本数据
        this.save(attrEntity);
        //保存关联关系
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrGroupId(attr.getAttrGroupId());
        relationEntity.setAttrId(attrEntity.getAttrId());
        relationDao.insert(relationEntity);
    }
    IPage<AttrEntity> page;
    PageUtils pageUtils;
    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId, String type) {
        params.forEach((key,value)->{
            System.out.println("每个键对应的值: "+key +" : "+value);
        });
        Set<String> set = params.keySet();
        set.forEach(t -> System.out.println(t));

        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>();//把前台提交的数据绑定到实体类attrEntity，然后封装数到queryWrapper
        String key = (String) params.get("key");
        if (catelogId != 0) {
            queryWrapper.eq("catelog_id", catelogId).eq("attr_type", "base".equalsIgnoreCase(type) ? 1 : 0);
            page = this.page(
                    new Query<AttrEntity>().getPage(params),
                    queryWrapper
            );
        }
        if(key != null){
            queryWrapper.and((wrapper)->{
                wrapper.eq("attr_id",key).or().like("attr_name",key);
            });
        }
        page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );
        pageUtils = new PageUtils(page);

//        String key = (String) params.get("key");
//        if(!StringUtils.isEmpty(key)){
//            //attr_id  attr_name
//            queryWrapper.and((wrapper)->{
//                wrapper.eq("attr_id",key).or().like("attr_name",key);
//            });
//        }

//        IPage<AttrEntity> page = this.page(
//                new Query<AttrEntity>().getPage(params),
//                queryWrapper
//        );
//
//        PageUtils pageUtils = new PageUtils(page);
//        List<AttrEntity> records = page.getRecords();
//        List<AttrRespVo> respVos = records.stream().map((attrEntity) -> {
//            AttrRespVo attrRespVo = new AttrRespVo();
//            BeanUtils.copyProperties(attrEntity, attrRespVo);

//            //1、设置分类和分组的名字
//            if("base".equalsIgnoreCase(type)){
//                AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
//                if (attrId != null && attrId.getAttrGroupId()!=null) {
//                    AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.getAttrGroupId());
//                    attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
//                }
//
//            }


//            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
//            if (categoryEntity != null) {
//                attrRespVo.setCatelogName(categoryEntity.getName());
//            }
//            return attrRespVo;
//        }).collect(Collectors.toList());
//
//        pageUtils.setList(respVos);
        return pageUtils;
    }


}